home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / color.c < prev    next >
Text File  |  1985-06-03  |  3KB  |  131 lines

  1. /****************************************************************************
  2.  *                                        *
  3.  *    COLOR.C     Max R. Dürsteler    12-7-83                *
  4.  *                                        *
  5.  *    Set screen colors in text mode.                     *
  6.  *                                        *
  7.  ****************************************************************************
  8.  */
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include "graph.h"
  12.  
  13. main(argc, argv)
  14.     int argc;
  15.     char **argv;
  16. {
  17.     int charcol = 11;
  18.     int scrncol = 4;
  19.     int bordcol = 4;
  20.     int i;
  21.  
  22.     if (argc < 4) {
  23.           printf("Usage: color [forground ] [background] [border]\n");
  24.           printf(" e.g.:\ncolor 11 4 12\n");
  25.           printf(" or:\ncolor yellow blue lblue\n");
  26.           printf(" or:\ncolor y blu lblu\n\n");
  27.           printf(" DOS 2.0 color indices: [0..15], background [0..7]\n\n");
  28.           printf(" Available color names: \n");
  29.           printf("  bla<ck> <l>r<ed> <l>g<reen> y<ellow> br<own>\n");
  30.           printf("  <l>blu<e> <l>m<agenta> <l>c<yan> <l>w<hite>\n\n");
  31.           printf(" <x> : x=optional character
  32.           printf("  l  : light (for foreground & border color only!)\n");
  33.           exit(0);
  34.     }
  35.     argv++;
  36.     charcol = colnum(*argv++);
  37.     scrncol = colnum(*argv++);
  38.     bordcol = colnum(*argv);
  39.     i = bordcol & 7;
  40.     switch (i) {        /* Translation from DOS 2.0 color table */
  41.         case 1:     /* to IBM color card color table */
  42.             i = 4;
  43.             break;
  44.         case 3:
  45.             i = 6;
  46.             break;
  47.         case 4:
  48.             i = 1;
  49.             break;
  50.         case 6:
  51.             i = 3;
  52.             break;
  53.         default:
  54.             i = bordcol;
  55.             break;
  56.     };
  57.     bordcol = i | (bordcol & 8);
  58.     screen(TEXTMOD);
  59.     outp(COLREG, bordcol | 0x10);
  60.     if (charcol & 8) printf("\033[1m\n");
  61.     else printf("\033[0m\n");
  62.     charcol = (charcol & 7) + 30;
  63.     scrncol = (scrncol & 7) + 40;
  64.     printf("\033[%d;%dm\n", charcol, scrncol);
  65.     cls();
  66. }
  67. /*----------------------------------------------------------------------*/
  68.  
  69. colnum(colname)
  70.     char *colname;
  71. {
  72.     int ncol;
  73.     int intens;
  74.     char *pc;
  75.  
  76.     pc = colname;
  77.     intens = 0;
  78.     if (isdigit(*pc)) {
  79.         if(sscanf(pc,"%d", &ncol) == 1)
  80.             return(ncol);
  81.         else {
  82.             fprintf(stderr,"what color number ? \n");
  83.             exit(0);
  84.         }
  85.     }
  86.     else if (isalpha(*pc)) {
  87.         if (*pc == 'l' || *pc == 'L') {       /* light ? */
  88.             intens = 8;
  89.             pc++;
  90.         }
  91.         switch(*pc) {
  92.             case 'b':
  93.             case 'B':
  94.                 if (*(pc + 1) == 'r' || *(pc + 1) == 'R')
  95.                     return(3);    /* brown */
  96.                 else switch(*(pc + 2)) {
  97.                     case 'a':       /* black */
  98.                     case 'A':
  99.                         return(0 | intens);
  100.                     case 'u':       /* blue */
  101.                     case 'U':
  102.                         return(4 | intens);
  103.                 };
  104.             case 'r':                       /* red */
  105.             case 'R':
  106.                 return(1 | intens);
  107.             case 'g':                       /* green */
  108.             case 'G':
  109.                 return(2 | intens);
  110.             case 'y':                       /* yellow */
  111.             case 'Y':
  112.                 return(11);
  113.             case 'm':        /* magenta */
  114.             case 'M':
  115.             case 'v':
  116.             case 'V':
  117.                 return(5 | intens);
  118.             case 'c':                       /* cyan */
  119.             case 'C':
  120.                 return(6 | intens);
  121.             case 'w':                       /* white */
  122.             case 'W':
  123.                 return(7 | intens);
  124.             default:
  125.                 fprintf(stderr,"what colour? \n");
  126.                 exit(0);
  127.         } /* end switch (*pc) */
  128.     } /* end else if (alpha) */
  129. } /* end subroutine colnum */
  130. /* end color.c */
  131.